/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.command.phonenumber;

import apus.command.AbstractCommand;
import apus.entity.PhoneNumber;
import apus.entity.Subscriber;
import apus.exception.BusinessException;
import apus.persistence.AbstractDAO;
import apus.persistence.OrganizationDAO;
import apus.persistence.PersonDAO;
import apus.services.ServiceLocator;
import java.util.Arrays;
import java.util.List;

/**
 * The <code>FindPhoneNumbersBySubscriberCommand</code> class represents command 
 * to search phone numbers for subscriber.
 * @author  Maxim Vasilevsky
 * @author  Roman Dyatkovsky
 * @since APUS v0.4
 * @see AbstractCommand
 */
public class FindPhoneNumbersBySubscriberCommand<TypeEn extends Subscriber,TypeDAO extends AbstractDAO<TypeEn>> extends AbstractCommand{

    private Class<TypeDAO> clazz;
    private final static String OWNER_ID = "Owner";
    
    private final static String[] paramsNames = { OWNER_ID };
    
    public FindPhoneNumbersBySubscriberCommand(Class<TypeDAO> clazz,String name) {
        super(name);
        this.clazz=clazz;
    }
    
    @Override
    public void execute() {     
        int ID;
        String stringId = params.get(OWNER_ID);
        try {
            ID= Integer.parseInt(stringId);
        } catch (NumberFormatException e) {
            throw new BusinessException("Wrong ID format: "+stringId, e);
        }
        
        AbstractDAO<TypeEn> SubDAO = ServiceLocator.getService(clazz);
        TypeEn sub=SubDAO.read(ID);
        
        if (sub==null){
            throw new BusinessException("Subscriber not found");
        }
        
        for(PhoneNumber num : sub.getPhoneNumbers()) {
            System.out.println("ID=" + num.getId() + " " + num.toString());
        }
        
    }
    
    @Override
    public List<String> getParamNames() {
        return Arrays.asList(paramsNames);
    }
    
    
    public static FindPhoneNumbersBySubscriberCommand getAddPhoneNumberToPersonCommand(){
        return new FindPhoneNumbersBySubscriberCommand(PersonDAO.class, "Find phone numbers by person");
    }
    
    public static FindPhoneNumbersBySubscriberCommand getAddPhoneNumberToOrganizationCommand(){
        return new FindPhoneNumbersBySubscriberCommand(OrganizationDAO.class, "Find phone numbers by organization");
    }
    
}
